package rpc.hook;

import lombok.extern.slf4j.Slf4j;
import rpc.factory.ThreadPoolFactory;
import rpc.util.NacosUtil;

import java.util.concurrent.ExecutorService;

/**
 * 钩子，自动注销服务
 *
 * @Author wupeijie
 * @Date 2020/12/3 17:38
 */
@Slf4j
public class ShutdownHook {
    private final ExecutorService threadPool = ThreadPoolFactory.createDefaultThreadPool("shutdown-hook");
    private static final ShutdownHook shutdownHook = new ShutdownHook();

    public static ShutdownHook getShutdownHook(){
        return shutdownHook;
    }

    public void addClearAllHook() {
        log.info("关闭后将自动注销所有服务");
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            NacosUtil.clearRegistry();
            threadPool.shutdown();
        }));
    }
}
